package com.lsz.deport.ui.login;

import androidx.annotation.NonNull;

import com.common.config.Global;
import com.common.config.MyApi;
import com.common.retrofit.RxObserver;
import com.common.retrofit.json.Data;
import com.lsz.deport.json.StartOrderBean;
import com.lsz.deport.json.UserEntity;
import com.util_code.base.mvp.RxMvpPresenter;
import com.util_code.utils.RxUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class LoginPresent extends RxMvpPresenter<LoginView> {

    protected MyApi mMyApi;

    @Inject
    public LoginPresent(MyApi myApi) {
        this.mMyApi = myApi;
    }

    void getCaptcha(String phone) {
        mMyApi.getCaptcha(phone)
                .compose(RxUtils.<Data>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data>() {

                    @Override
                    public void onNext(@NonNull Data data) {
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }
                });
    }

    void postLogin(String phone, String captcha) {
        Map<String, Object> param = new HashMap<>();
        param.put("phone", phone);
        param.put("captcha", captcha);
        mMyApi.postLogin(onRequestBody(param))
                .compose(RxUtils.<Data<UserEntity>>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<UserEntity>>() {

                    @Override
                    public void onNext(@NonNull Data<UserEntity> data) {
                        if (checkJsonCode(data, true) && data.data != null) {
                            Global.setUserBean(data.data);
                            getView().onSuccess(data.data);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().onFali();
                    }
                });
    }

}
